//
//  HSTabBarController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSTabBarController: UITabBarController {
    //全局闭包属性
    var leftBtnBlock:(() -> ())?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
      //
        view.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
        //改变按钮字体选中颜色---调用延展的方法
        tabBar.tintColor = UIColor().colorWithHex(0x00a0e9)
   
        //通过反射机制添加控制器
        let tabDict = [["vc":"HSMessageController", "title": "消息", "imageName": "inform"],["vc":"HSHomeWorkController", "title": "作业", "imageName": "homework"],["vc":"HSClassRoomController", "title": "课堂", "imageName": "class_exercise"],["vc":"HSScoreController", "title": "成绩", "imageName": "examination"]]
        for dict in tabDict{
            addChildViewController(dict: dict)
        }
        
    }
    //重载添加方法封装
    //fileprivate 私有化 支持延展
    //private 完全私有，只在当前类方法中有效
   fileprivate func addChildViewController(dict : [String : String]) {
        
        // 获得系统----到  获得当前类型  是用系统服务器传来的数据来创建对应的控制器--就是反射机制
        
        //获得命名空间并将控制器名称转换类 ---Bundle的命名空间是通过延展添加的属性获得的。
       guard let bundName = Bundle.main.bundleName ,
        let imageName = dict["imageName"] ,
        let vcName = dict["vc"] ,
         //使用反射机制将字符串转换为类
        let cls = NSClassFromString("\(bundName).\(vcName)") as? UIViewController.Type else {
            return
        }
     
      

        //获得当前类型
        let childController = cls.init()
        
//        print(className)
        //标题
        childController.title = dict["title"]
        //图片--normal
        childController.tabBarItem.image = UIImage(named: "\(imageName)_nor")
        //select图片--- 并且设置为原色绘制
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_press")?.withRenderingMode(.alwaysOriginal)
        //设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: .normal)

        
        //重写方法
        childController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "icon_menu_nor", bgImageName: "icon_menu_press", target: self, action: #selector(leftBtnClicked))
//        childController.navigationItem.rightBarButtonItem = UIBarButtonItem.init(imageName: "release_msg_nor", bgImageName: "release_msg_nor_press")
        
        //导航控制器
        let navVc = HSNavigationController(rootViewController: childController)
        
        //添加视图
        addChildViewController(navVc)
    }
    //左边按钮的监听方法
   @objc fileprivate func leftBtnClicked(){
    //执行闭包传递点击事件
       leftBtnBlock?()
    }
    
}
